package com.example.treehole.ui.model

import android.content.Context
import android.content.SharedPreferences

class UserPreferences(context: Context) {
    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE)

    companion object {
        private const val USER_ID = "user_id"
        private const val JWL_TOKEN = "jwl_token"
    }

    fun saveLoginInfo(
        userId: Int,
        jwlToken: String
    ) {
        val editor = sharedPreferences.edit()
        editor.putInt(USER_ID, userId)
        editor.putString(JWL_TOKEN, jwlToken)
        editor.apply()
    }

    fun getUserId(): Int {
        return sharedPreferences.getInt(USER_ID, -1)
    }

    fun getJwlToken(): String {
        return sharedPreferences.getString(JWL_TOKEN, "")?:""
    }

    fun clearLoginInfo() {
        val editor = sharedPreferences.edit()
        editor.remove(USER_ID)
        editor.remove(JWL_TOKEN)
        editor.apply()
    }

    fun isLoggedIn(): Boolean {
        return getUserId() != -1 && getJwlToken() != ""
    }
}